<?

class Image
{
    private $imageFile;
    
    public function __construct($file)
    {
        $this->imageFile = $file;
    }

    public function getSize()
    {
        $data = @getimagesize($this->imageFile);
        if($data)
            return array($data[0], $data[1]);
        else
            throw new ImageException("Error while geting image size: ".$this->imageFile);
    }

    /**
     * Resize image to fit in given dimensions keeping original dimensions ratio
     * 
     * @throws ImageException on image manipulation error
     */
    public function resizeMax($width = null, $height = null, $fileOut = null)
    {
        $fileIn = $this->imageFile;
        // redirects to STDOUT if fileOut is null
        if(!isset($fileOut))
            $fileOut = "-";

        $rv = null; // will contain the return value of the command
        system("convert -resize $width"."x$height $fileIn $fileOut >/dev/null 2>&1", $rv);
        if($rv)
            throw new ImageException("Error while resizing image");
    }

    /**
     * Resize image to given dimensions by truncating the image if dimensions ratio
     * doesn't correspond
     *
     * @throws ImageException on image manipulation error
     */
    public function resizeTruncate($width = null, $height = null, $fileOut = null)
    {
        $fileIn = $this->imageFile;
        // redirects to STDOUT if fileOut is null
        if(!isset($fileOut))
            $fileOut = "-";

        $temp = tempnam("/tmp", "Image");

        list($origWidth, $origHeight) = $this->getSize();

        $widthArg = null;
        $heightArg = null;
        
        if(($origWidth / $origHeight) > ($width / $height))
            $heightArg = $height;
        else
            $widthArg = $width;

        try
        {
            $this->resizeMax($widthArg, $heightArg, $temp);
        }
        catch(ImageException $e)
        {
            @unlink($temp);
            throw $e;
        }

        list($newWidth, $newHeight) = getimagesize($temp);

        if($newWidth > $newHeight)
        {
            $cropX = round(($newWidth - $width) / 2);
            $cropY = 0;
        }
        else
        {
            $cropX = 0;
            $cropY = round(($newHeight - $height) / 2);
        }

        $rv = null; // will contain the return value of the command
        system("convert -crop $width"."x$height+$cropX+$cropY $temp $fileOut >/dev/null 2>&1", $rv);
        @unlink($temp);
        if($rv)
            throw new ImageException("Error while croping image");
    }
    
    /**
     * Crop and resize image to given coords by zooming and cropping the image
     *
     * @throws ImageException on image manipulation error
     */
    public function cropResize($x1, $y1, $x2, $y2,  $width = null, $height = null, $fileOut = null)
    {
        $fileIn = $this->imageFile;
        // redirects to STDOUT if fileOut is null
        if(!isset($fileOut))
            $fileOut = "-";

        $temp = tempnam("/tmp", "Image");

        list($origWidth, $origHeight) = $this->getSize();
        
        $widthCrop = $x2 - $x1;
        $heightCrop = $y2 - $y1;

        $ratioW = $width / $widthCrop;
        $ratioH = $height / $heightCrop;

        if($ratioW != 0 && $ratioH != 0 && $ratioW != $ratioH)
            throw new ImageException("Source and destination diensions are not proportional");

        if($ratioW + $ratioH == 0)
            throw new ImageException("Neither width nor heigt was given, at least one of them is mandatory");
        $ratio = max($ratioW, $ratioH);        

        $zoomedWidth = $origWidth * $ratio;
        $zoomedHeight = $origHeight * $ratio;

        $cropX = $x1 * $ratio;
        $cropY = $y1 * $ratio;

        try
        {
            $this->resizeMax($zoomedWidth, $zoomedHeight, $temp);
        }
        catch(ImageException $e)
        {
            @unlink($temp);
            throw $e;
        }

        $rv = null; // will contain the return value of the command
        system("convert -crop $width"."x$height + $cropX +$cropY $temp $fileOut >/dev/null 2>&1", $rv);
        @unlink($temp);
        if($rv)
            throw new ImageException("Error while croping image");
    }
}

class ImageException extends Exception {}



imageFile = $file; } public function getSize() { $data = @getimagesize($this->imageFile); if($data) return array($data[0], $data[1]); else throw new ImageException("Error while geting image size: ".$this->imageFile); } /** * Resize image to fit in given dimensions keeping original dimensions ratio * * @throws ImageException on image manipulation error */ public function resizeMax($width = null, $height = null, $fileOut = null) { $fileIn = $this->imageFile; // redirects to STDOUT if fileOut is null if(!isset($fileOut)) $fileOut = "-"; $rv = null; // will contain the return value of the command system("convert -resize $width"."x$height $fileIn $fileOut >/dev/null 2>&1", $rv); if($rv) throw new ImageException("Error while resizing image"); } /** * Resize image to given dimensions by truncating the image if dimensions ratio * doesn't correspond * * @throws ImageException on image manipulation error */ public function resizeTruncate($width = null, $height = null, $fileOut = null) { $fileIn = $this->imageFile; // redirects to STDOUT if fileOut is null if(!isset($fileOut)) $fileOut = "-"; $temp = tempnam("/tmp", "Image"); list($origWidth, $origHeight) = $this->getSize(); $widthArg = null; $heightArg = null; if(($origWidth / $origHeight) > ($width / $height)) $heightArg = $height; else $widthArg = $width; try { $this->resizeMax($widthArg, $heightArg, $temp); } catch(ImageException $e) { @unlink($temp); throw $e; } list($newWidth, $newHeight) = getimagesize($temp); if($newWidth > $newHeight) { $cropX = round(($newWidth - $width) / 2); $cropY = 0; } else { $cropX = 0; $cropY = round(($newHeight - $height) / 2); } $rv = null; // will contain the return value of the command system("convert -crop $width"."x$height+$cropX+$cropY $temp $fileOut >/dev/null 2>&1", $rv); @unlink($temp); if($rv) throw new ImageException("Error while croping image"); } /** * Crop and resize image to given coords by zooming and cropping the image * * @throws ImageException on image manipulation error */ public function cropResize($x1, $y1, $x2, $y2, $width = null, $height = null, $fileOut = null) { $fileIn = $this->imageFile; // redirects to STDOUT if fileOut is null if(!isset($fileOut)) $fileOut = "-"; $temp = tempnam("/tmp", "Image"); list($origWidth, $origHeight) = $this->getSize(); $widthCrop = $x2 - $x1; $heightCrop = $y2 - $y1; $ratioW = $width / $widthCrop; $ratioH = $height / $heightCrop; if($ratioW != 0 && $ratioH != 0 && $ratioW != $ratioH) throw new ImageException("Source and destination diensions are not proportional"); if($ratioW + $ratioH == 0) throw new ImageException("Neither width nor heigt was given, at least one of them is mandatory"); $ratio = max($ratioW, $ratioH); $zoomedWidth = $origWidth * $ratio; $zoomedHeight = $origHeight * $ratio; $cropX = $x1 * $ratio; $cropY = $y1 * $ratio; try { $this->resizeMax($zoomedWidth, $zoomedHeight, $temp); } catch(ImageException $e) { @unlink($temp); throw $e; } $rv = null; // will contain the return value of the command system("convert -crop $width"."x$height + $cropX +$cropY $temp $fileOut >/dev/null 2>&1", $rv); @unlink($temp); if($rv) throw new ImageException("Error while croping image"); } } class ImageException extends Exception {} ?>
